import { ExtendedApiClient } from './api'
import { getEnhancedApiClient } from './enhancedMockApi'
import { createDevApiClient } from './devApiClient'
import { useDevToolsStore } from '@/stores/devToolsStore'

// API客户端工厂
export class ApiClientFactory {
    private static instance: ExtendedApiClient | null = null
    private static currentMode: string | null = null

    // 获取API客户端实例
    static getApiClient(): ExtendedApiClient {
        // 检查是否需要重新创建客户端（模式变化）
        const currentDevMode = this.getCurrentMode()

        if (!this.instance || this.currentMode !== currentDevMode) {
            this.instance = this.createApiClient()
            this.currentMode = currentDevMode

            console.log(`🔄 API Client switched to mode: ${currentDevMode}`)
        }

        return this.instance
    }

    // 获取当前模式
    private static getCurrentMode(): string {
        if (typeof window === 'undefined') {
            return 'server' // 服务端渲染
        }

        const devStore = useDevToolsStore.getState()

        if (process.env.NODE_ENV === 'development' && devStore.isEnabled) {
            if (devStore.useMockApi) {
                return `mock-${devStore.mockDataMode}`
            }
            return 'dev-real'
        }

        return 'production'
    }

    // 创建API客户端
    private static createApiClient(): ExtendedApiClient {
        const mode = this.getCurrentMode()

        if (mode.startsWith('mock-')) {
            // 使用增强的模拟API
            return getEnhancedApiClient()
        } else if (mode === 'dev-real') {
            // 开发环境使用带调试功能的真实API
            return createDevApiClient()
        } else {
            // 生产环境或服务端使用标准API
            return this.createProductionApiClient()
        }
    }

    // 创建生产环境API客户端
    private static createProductionApiClient(): ExtendedApiClient {
        // 直接导入真实API客户端
        // eslint-disable-next-line @typescript-eslint/no-require-imports
        const { api } = require('./api')
        return api
    }

    // 强制刷新API客户端（用于开发工具切换）
    static refreshApiClient(): void {
        this.instance = null
        this.currentMode = null
        console.log('🔄 API Client cache cleared')
    }

    // 获取当前API客户端信息
    static getClientInfo(): {
        mode: string
        useMockApi: boolean
        mockDataMode?: string
        isDevMode: boolean
    } {
        const mode = this.getCurrentMode()
        const devStore = typeof window !== 'undefined'
            ? useDevToolsStore.getState()
            : { useMockApi: false, mockDataMode: 'default', isEnabled: false }

        return {
            mode,
            useMockApi: devStore.useMockApi,
            mockDataMode: devStore.mockDataMode,
            isDevMode: process.env.NODE_ENV === 'development' && devStore.isEnabled
        }
    }
}

// 便捷的API客户端获取函数
export const getApiClient = (): ExtendedApiClient => {
    return ApiClientFactory.getApiClient()
}

// 刷新API客户端的便捷函数
export const refreshApiClient = (): void => {
    ApiClientFactory.refreshApiClient()
}

// 获取API客户端信息的便捷函数
export const getApiClientInfo = () => {
    return ApiClientFactory.getClientInfo()
}

// 默认导出
export default ApiClientFactory